package com.lianniu.mgr.product.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.product.model.ProductBrandVo;
import com.lianniu.mgr.product.service.IProductBrandService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/rest/ProductBrand")
public class ProductBrandRestController {
	
	private final static Logger logger = Logger.getLogger(ProductBrandRestController.class);
	  
	
	  @Autowired
	  private IProductBrandService ProductBrandService;
	  
	  @RequestMapping(value="/ProductBrandList",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult ProductBrandList(HttpServletRequest request, ProductBrandVo curVo, PaginationInfo paginationInfo)
	  {
	       logger.info("ProductBrandRestController  ProductBrandList begin.");
	       try {
	           
	           logger.info("ProductBrandList begin.");          
	           BaseResult baseResult = ProductBrandService.selectByProductBrandForPage(curVo, paginationInfo);
	           logger.info("ProductBrandList end.");
	           logger.info("ProductBrandRestController ProductBrandList end.");
	           return baseResult;
	       } catch (Exception e) {
	       logger.error("ProductBrandRestController ProductBrandList error:" + e.getMessage());
	       logger.error("ProductBrandRestController ProductBrandList end.");
	       return BaseResult.exception(e.getMessage());
	     }
	  }
	  
	  @RequestMapping(value = {"/ProductBrandAdd"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult dictionaryAdd(HttpServletRequest request,ProductBrandVo curVo) {
	      logger.info("ProductBrandRestController ProductBrandAdd begin.");      
	      logger.info("ProductBrandRestController ProductBrandAdd:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("ProductBrandAdd begin.");
	        
	          BaseResult baseResult = ProductBrandService.insertProductBrand(curVo);
	          logger.info("ProductBrandAdd baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ProductBrandAdd end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductBrandAdd error:"+e.getMessage());
	      logger.info("ProductBrandRestController ProductBrandAdd end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  } 
	  
	  
	  @RequestMapping(value = {"/ProductBrandEdit"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult ProductBrandEdit(HttpServletRequest request,ProductBrandVo curVo) {
	      logger.info("ProductBrandRestController ProductBrandEdit begin.");      
	      logger.info("ProductBrandRestController ProductBrandEdit:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("ProductBrandEdit begin.");
	          BaseResult baseResult = ProductBrandService.updateById(curVo);
	          logger.info("ProductBrandEdit baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ProductBrandEdit end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductBrandEdit error:"+e.getMessage());
	      logger.info("ProductBrandRestController ProductBrandEdit end.");
	      return BaseResult.exception(e.getMessage());
	    }
	  }  
	  
	  
	  @RequestMapping(value="/selectProductBrandById",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectProductBrandById(HttpServletRequest request,ProductBrandVo curVo) {
	        logger.info("ProductBrandRestController selectProductBrandById begin.");
	        try {
	                    
	          BaseResult baseResult = ProductBrandService.selectById(curVo);
	          logger.info("selectDictionaryById end.");
	          logger.info("ProductBrandRestController selectProductBrandById end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ProductBrandRestController selectProductBrandById error:" + e.getMessage());
	          logger.error("ProductBrandRestController selectProductBrandById end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	  
	  @RequestMapping(value = {"/ProductBrandDel"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult ProductBrandDel(HttpServletRequest request) {
	      logger.info("ProductBrandRestController ProductBrandDel begin.");
	     try {          
	          logger.info("ProductBrandDel begin.");
	          BaseResult baseResult = ProductBrandService.deleteProductBrandBatch(request.getParameter("ids").split(","));		
	          logger.info("ProductBrandDel baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ProductBrandDel end.");
	          
	          logger.info("ProductBrandRestController ProductBrandDel end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductBrandRestController ProductBrandDel error:" + e.getMessage());
	      logger.info("ProductBrandRestController ProductBrandDel end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	  
	  @RequestMapping(value="/selectParentBrand",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectParentCate(HttpServletRequest request) {
	        logger.info("ProductCateRestController selectParentCate begin.");
	        try {
	        	
	          String cateId = request.getParameter("cateId");          
	          BaseResult baseResult = ProductBrandService.selectParentBrand(cateId);
	          logger.info("selectParentCate end.");
	          logger.info("ProductCateRestController selectParentCate end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ProductCateRestController selectParentCate error:" + e.getMessage());
	          logger.error("ProductCateRestController selectParentCate end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	  
	  @RequestMapping(value="/selectChildBrandKv",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectChildBrandKv(HttpServletRequest request) {
	        logger.info("ProductCateRestController selectChildCateKv begin.");	        
	        String brandId = request.getParameter("brandId");
	        logger.info("ProductCateRestController selectChildCateKv cateId:" + JSON.toJSONString(brandId));
	        try {	                    
	          BaseResult baseResult = ProductBrandService.selectChildBrandKV(brandId);
	          logger.info("selectChildCateKv end.");
	          logger.info("ProductCateRestController selectChildCateKv end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ProductCateRestController selectChildCateKv error:" + e.getMessage());
	          logger.error("ProductCateRestController selectChildCateKv end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	  
	  
	  @RequestMapping(value = {"/checkCateName"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkCateName(HttpServletRequest request) {
	      Boolean flag=true;
	      String cateName=  request.getParameter("cateName");
	      logger.info("ProductBrandRestController checkCateName begin.");
	     try {          
	          logger.info("checkCateName begin.");
	          if("".equalsIgnoreCase(cateName)||cateName==null)
	          {return flag;}
	          flag = ProductBrandService.checkCateName(cateName);         
	          logger.info("checkCateName end.");
	          
	          logger.info("ProductBrandRestController checkCateName end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("ProductBrandRestController checkCateName error:" + e.getMessage());
	      logger.info("ProductBrandRestController checkCateName end.");
	      return flag;
	    }

	  }
	  
	  
	  @RequestMapping(value = {"/checkCateNameEdit"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkCateNameEdit(HttpServletRequest request) {
	      Boolean flag=true;
	      String cateName=  request.getParameter("cateName");
	      String cateId=request.getParameter("cateId");
	      logger.info("ProductBrandRestController checkCateNameEdit begin.");
	     try {          
	          logger.info("checkTypeNameEdit begin.");
	          ProductBrandVo vo=new ProductBrandVo();	         
	          vo.setCateId(cateId);
	          
	          BaseResult baseResult =ProductBrandService.selectById(vo);	          
	          ProductBrandVo curVo=(ProductBrandVo) baseResult.getData();
	          if(curVo.getCateId().equals(cateId)&&curVo.getCateName().equals(cateName))
	          {return flag;}
	          if("".equalsIgnoreCase(cateName)||cateName==null)
	          {return flag;}
	          flag = ProductBrandService.checkCateName(cateName);         
	          logger.info("checkCateNameEdit end.");
	          
	          logger.info("ProductBrandRestController checkCateNameEdit end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("ProductBrandRestController checkCateNameEdit error:" + e.getMessage());
	      logger.info("ProductBrandRestController checkCateNameEdit end.");
	      return flag;
	    }

	  }

}
